package com.wxy.config;

import com.wxy.dao.SecurityUserMapper;
import com.wxy.domain.SecurityUser;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
/**
 * @author 47868
 */
@Configuration
public class UserDetailsConfig implements UserDetailsService {
    @Resource
    private SecurityUserMapper securityUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SecurityUser securityUser=new SecurityUser();
        securityUser=securityUserMapper.findByUsername(username);
        if(securityUser==null){
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        return User
                .withUsername(securityUser.getUsername())
                .password(securityUser.getPassword())
                .roles(securityUser.getRole())
                .build();
    }
}
